//
//  JOTableHeader.m
//  qq好友
//
//  Created by song on 15/1/19.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOTableHeader.h"
#import "JOFriendGroup.h"

@interface JOTableHeader ()

@property(nonatomic ,weak) UIButton *nameview;

@property(nonatomic, weak )UILabel *countview;


@end

@implementation JOTableHeader


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithReuseIdentifier:reuseIdentifier])
    {
        UIButton *nameview = [[UIButton alloc]init];
        [nameview setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        [nameview setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateNormal];
        [nameview setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        //设置文本的对齐
        nameview.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [nameview setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        nameview.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        nameview.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        
        //内容的模式居中
        nameview.imageView.contentMode =UIViewContentModeCenter;
        //超出的内容不进行裁剪
        nameview.imageView.clipsToBounds =NO;
        [self.contentView addSubview:nameview];
        [nameview addTarget:self action:@selector(nameclick:) forControlEvents:UIControlEventTouchUpInside];
        self.nameview = nameview;
        
        UILabel *countview =[[UILabel alloc]init];
        countview.textAlignment = NSTextAlignmentRight;
        countview.text=@"1/10";
        countview.textColor =[UIColor grayColor];
        [self.contentView addSubview:countview];
        self.countview =countview;
    }
    return  self;
}

+(instancetype)headerViewWithTableView:(UITableView *)tableView
{
    static NSString *ID=@"header";
    JOTableHeader *header =[tableView dequeueReusableCellWithIdentifier:ID];
    if(header == nil)
    {
        header = [[JOTableHeader alloc]initWithReuseIdentifier:ID];
    }
    return header;
}

-(void)setGroup:(JOFriendGroup *)group
{
    _group =group;
    
    [self.nameview setTitle:group.name forState:UIControlStateNormal];
    
    self.countview.text  = [NSString stringWithFormat:@"%d/%d",group.online ,(int) group.friends.count];
}

/**
 *  调整布局的 frame
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.nameview.frame = self.bounds;
    
    
    CGFloat countY=0;
    CGFloat countH = self.frame.size.height;
    CGFloat countW =150;
    CGFloat countX = self.frame.size.width -10 -countW;
    self.countview.frame = CGRectMake(countX, countY, countW, countH);
    
}

-(void)nameclick:(UIButton *)btn
{
    self.group.opened = !self.group.opened;
    NSLog(@" header is click ");
    if([self.delegate respondsToSelector:@selector(headViewClickName:)] )
    {
        [self.delegate headViewClickName:self];
    }
   
    
    
}

-(void)didMoveToSuperview
{
    if(self.group.opened)
    {
        self.nameview.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }else{
        self.nameview.imageView.transform = CGAffineTransformMakeRotation(0);
    }
}

@end
